﻿using MediatR;
using Newtonsoft.Json;
using Production_Get.API.Infrastructures.Interfaces;
using Return.Enum;
using THE_WIND_RISES.Common.Domans.BasicInformation.Materials;
using THE_WIND_RISES.Common.Domans.BasicInformation.warehouses;
using THE_WIND_RISES.Common.Dtos;
namespace Production_Get.API.Application.Commands.WorkCenter_W
{
    /// <summary>
    /// 库存清单的帆软报表导出
    /// </summary>
    public class WorMShowHandler : IRequestHandler<WorMShowCommand, Result>
    {
        private readonly IBaseRepositoryGet<ItemInfo> ItemInfoRepository;
        private readonly IBaseRepositoryGet<Warehouse> WarehouseRepository;
        private readonly IBaseRepositoryGet<WorM> WorMRepository;
        private readonly ILogger<WorMShowHandler> logger;

        public WorMShowHandler(IBaseRepositoryGet<ItemInfo> itemInfoRepository, IBaseRepositoryGet<Warehouse> warehouseRepository, ILogger<WorMShowHandler> logger, IBaseRepositoryGet<WorM> worMRepository)
        {
            ItemInfoRepository = itemInfoRepository;
            WarehouseRepository = warehouseRepository;
            this.logger = logger;
            WorMRepository = worMRepository;
        }

        public async Task<Result> Handle(WorMShowCommand request, CancellationToken cancellationToken)
        {
            var result = new THE_WIND_RISES.Common.Dtos.Result() { Code = (int)CommonErrorCodes.Search };
            try
            {
                var sql = "select * from WorM";
                var sql1 = "select * from ItemInfo";
                var sql2 = "select * from Warehouse";
                var list = from a in WorMRepository.GetAll(sql)
                           join b in ItemInfoRepository.GetAll(sql1)
                           on a.InventoryInfoId equals b.InventoryInfoId
                           join c in WarehouseRepository.GetAll(sql2)
                           on a.WarehouseId equals c.WarehouseId
                           select new
                           {
                               c.WarehouseId,
                               c.WarehousNumBer,//仓库编码
                               c.WarehouseName,//仓库名称
                               b.InventoryInfoId,
                               b.ItemCode,//物料编码
                               b.ItemName,//物料名称
                               b.SpecName,//物料规格
                               b.ItemServeId,//批号
                               b.InventoryLimit,//（库存上限）库存数量
                               danwei = "无",
                               chengben = 50,
                               sum = 50 * b.InventoryLimit
                           };
                if (request.InventoryInfoId > 0)
                {
                    list = list.Where(x => x.InventoryInfoId == request.InventoryInfoId);
                }
                if (request.WarehouseId > 0)
                {
                    list = list.Where(x => x.WarehouseId == request.WarehouseId);
                }
                if (list.Count() > 0)
                {
                    result.Data = list;
                    result.Code = (int)CommonErrorCodes.Success;
                }
                
            }
            catch (Exception ex)
            {
                logger.LogError($"工作中心显示列表查询失败," + ex.Message);
                throw;
            }
            return result;
        }
    }
}
